package com.aop.advice;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;

import com.aop.annotaions.FilterAnnotaions;

@Aspect
public  class AbstractFilter {
	
	
	@Pointcut("@annotation(com.aop.annotaions.FilterAnnotaions)")
    public void filterAnnotaions() {
    }
	
	@Around("filterAnnotaions()")
	public Object aroundParameterFormat(ProceedingJoinPoint pjp) throws Throwable{
		System.out.println(1111111);
		Method method = null;
		Object cc=null;
		 try {
			 try {
				 method = getCurrentMethod(pjp);
			} catch (Exception e) {
				e.printStackTrace();
				throw new RuntimeException("获取参数变形切入点失败",e);
			}
			 
			FilterAnnotaions annotation = method.getAnnotation(FilterAnnotaions.class);
			String invoker = annotation.invoker();
			annotation.aaa();
			System.out.println(invoker);
			 
//		 	ParameterFormat annotation = method.getAnnotation(ParameterFormat.class);
//			if(annotation!=null){//理论上不会为null
//				invoker = annotation.invoker();
//				if(EnumFormatModel.REQUEST==invoker||EnumFormatModel.BOTH==invoker){//入参变形
//					newArgs = formatRequest(pjp, method);
//				}
//			}
			try {
				  cc = pjp.proceed();
				
				//ret = (R)(newArgs==null?pjp.proceed():pjp.proceed(newArgs));
			} catch (Throwable t) {
				throw t;
			}
		}catch (Throwable t) {
			throw t;
		}
		
		return cc;
	}
	
	
	{
		
		System.out.println("AbstractFilter static ");
	}
	
	public void init (){
		
		
		System.out.println("AbstractFilter init ");
	}
	
	
	   //获得切点方法
    protected Method getCurrentMethod(ProceedingJoinPoint pjp) throws Throwable {
        Signature sig = pjp.getSignature();
        MethodSignature msig = null;
        if (!(sig instanceof MethodSignature)) {
            throw new IllegalArgumentException("该注解只能用于方法");
        }
        msig = (MethodSignature) sig;
        Object target = pjp.getTarget();
        Method currentMethod = target.getClass().getMethod(msig.getName(), msig.getParameterTypes());
        return currentMethod;
    }
    
    @SuppressWarnings({ "unchecked", "rawtypes" })
    //获得方法上的注解
    protected Annotation getMethodAnnotation(Method method, Class annotationClass) {
        return method.getAnnotation(annotationClass);
    }
    
    //获得注解某个方法的值
    protected Object quietGetFromAnnotation(String methodName, Annotation annotation) {
        if (annotation == null) {
            return null;
        }
        try {
            return annotation.annotationType().getDeclaredMethod(methodName).invoke(annotation);
        } catch (Exception e) {
           e.printStackTrace();
        }
        return null;
    }
    
  
    
}
